import tkinter as tk
from tkinter import Toplevel, Text, Scrollbar
from tkinter.font import Font
import json

def show_all_cards(root):
    # 创建一个新的Toplevel窗口
    top = Toplevel(root)
    top.title("所有卡牌列表")

    # 设置窗口大小
    top.geometry("400x300")

    # 读取卡片数据
    def load_cards():
        with open('card.json', 'r', encoding='utf-8') as file:
            return json.load(file)['cards']

    cards = load_cards()

    # 创建一个字符串，用于显示所有卡片信息
    card_list_str = "id\tname\tdescription\n"
    for card in cards:
        card_list_str += f"{card['id']}\t{card['name']}\t{card['description']}\n"

    # 创建一个文本框，用于显示卡片列表
    button_font = Font(size=12)
    card_list_text = Text(top, width=40, height=10, wrap='word', font=button_font)
    card_list_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
    card_list_text.insert(tk.END, card_list_str)

    # 滚动条
    scroll = Scrollbar(top, command=card_list_text.yview)
    scroll.pack(side=tk.RIGHT, fill=tk.Y)
    card_list_text.config(yscrollcommand=scroll.set)

    # 绑定滚动条与文本框
    card_list_text.bind("<MouseWheel>", lambda event: card_list_text.yview(tk.SCROLL, f"{-1*event.delta}", tk.UNITS))
    card_list_text.bind("<Button-4>", lambda event: card_list_text.yview(tk.SCROLL, tk.SCROLL, 1))
    card_list_text.bind("<Button-5>", lambda event: card_list_text.yview(tk.SCROLL, tk.SCROLL, -1))

    # 禁止文本框编辑
    card_list_text.config(state=tk.DISABLED)